package com.greenbook.controller.user;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.greenbook.dto.LikeOrCollectDTO;
import com.greenbook.dto.NoteDTO;
import com.greenbook.dto.PageDTO;
import com.greenbook.result.Result;
import com.greenbook.service.NoteService;
import com.greenbook.utils.MinioUtil;
import com.greenbook.vo.NoteVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/note")
@Tag(name = "笔记相关接口")
@RequiredArgsConstructor
@Slf4j
public class NoteController {
    private final NoteService noteService;
    private final MinioUtil minioUtil;

    /**
     * 保存笔记
     *
     * @param files
     * @param noteData
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "保存笔记")
    public Result save(@RequestParam("uploadFiles") MultipartFile[] files, @RequestParam("noteData") String noteData) {
        log.info("保存笔记:{}", noteData);
        // 处理文件上传
        List<String> fileList = new ArrayList<>();
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String fileName = UUID.randomUUID().toString() + "." + extension;
            fileList.add(minioUtil.upload(file, fileName));
        }
        // 处理笔记数据
        NoteDTO noteDTO = new NoteDTO();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            noteDTO = objectMapper.readValue(noteData, NoteDTO.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        noteDTO.setFileList(fileList);
        noteService.save(noteDTO);
        return Result.success();
    }

    /**
     * 根据笔记id获取笔记
     *
     * @param noteId
     * @return
     */
    @GetMapping("/{noteId}")
    @Operation(summary = "根据笔记id获取笔记")
    public Result<NoteVO> getNoteById(@PathVariable("noteId") Long noteId) {
        log.info("根据笔记id获取笔记{}", noteId);
        return Result.success(noteService.getNoteById(noteId));
    }

    /**
     * 根据用户id获取笔记列表
     */
    @GetMapping("/pageByUserId/{currentPage}/{pageSize}")
    @Operation(summary = "根据用户id获取笔记列表")
    public Result<PageDTO> pageByUserId(@PathVariable("currentPage") Integer currentPage,
                                                     @PathVariable("pageSize") Integer pageSize,
                                                     @RequestParam("userId") Long userId,
                                                     @RequestParam("type") Integer type) {
        return Result.success(noteService.pageByUserId(currentPage, pageSize, userId, type));
    }

    /**
     * 文章点赞
     */
    @PostMapping("/likeOrCollect")
    @Operation(summary = "文章点赞")
    public Result likeOrCollect(@RequestBody LikeOrCollectDTO likeOrCollectDTO) {
        log.info("文章点赞:{}", likeOrCollectDTO);
        noteService.likeOrCollect(likeOrCollectDTO);
        return Result.success();
    }
}
